<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        input, select {
            width: 600px;
            height: 30px;
        }

        .mainContainer {
            display: block;
            width: 200px;
            margin-left: auto;
            margin-right: auto;
        }

        .urlInput {
            display: block;
            width: 100%;
            margin-left: auto;
            margin-right: auto;
            margin-top: 8px;
            margin-bottom: 8px;
        }

        .centeredVideo {
            display: block;
            width: 100%;
            height: 100px;
            margin-left: auto;
            margin-right: auto;
            margin-bottom: auto;
        }

        .controls {
            display: block;
            width: 100%;
            text-align: left;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body>
新增视频：<input type="button" value="新增" onclick="addVideo()">
<br/>
视频列表：<select id="videoSelect" onchange="setWebSocketUrl()"></select>
<br/>
websocket地址：<input type="text" id="url">
<br/>
推流：<input type="button" value="推流" onclick="changeStatus(1)">
<br/>
关流：<input type="button" value="关流" onclick="changeStatus(0)">
<br/>
<div class="controls">
    <!--<button onclick="flv_load()">加载</button>-->
    <button onclick="flv_start()">开始</button>
    <button onclick="flv_pause()">暂停</button>
    <button onclick="flv_change()">切换</button>
</div>
<div class="mainContainer">
    <video id="videoElement" class="centeredVideo" controls autoplay width="200" height="100">Your browser is too old
        which doesn't support HTML5 video.
    </video>
</div>
</body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
    function showWebSocketUrl() {
        $("#url").val($("#videoSelect").val());
    }

    function getList() {
        $.ajax({
            url: "/list",
            type: "GET",
            data: {},
            dataType: "JSON",
            success: function (response) {
                if (response.code === 200) {
                    var html = "";
                    for (let i = 0; i < response.data.length; i++) {
                        var item = response.data[i];
                        html += `<option value="ws://127.0.0.1:8866/live?url=${item.url}" data-url="${item.url}">${item.url}--${item.status ? '已开启' : '已关闭'}</option>`;
                    }
                    $("#videoSelect").empty().append(html);
                    showWebSocketUrl();
                }
            }
        });
    }

    function setWebSocketUrl() {
        $("#url").val($("#videoSelect").val());
    }

    function addVideo() {
        var url = prompt("请输入URL");
        if (!url) {
            alert("请输入URL");
            return;
        }
        $.ajax({
            url: "/add",
            type: "GET",
            data: {
                url: url,
                remark: "test"
            },
            dataType: "JSON",
            success: function (response) {
                if (response.code === 200) {
                    alert(response.msg);
                    getList();
                }
            }
        });
    }

    function changeStatus(type, stopAll) {
        var url = $("#videoSelect option:selected").data("url");
        if (type === 1) {
            //播放
            $.ajax({
                url: "/start",
                type: "GET",
                data: {
                    url: url
                },
                dataType: "JSON",
                success: function (response) {
                    if (response.code === 200) {
                        alert(response.msg);
                        setTimeout(function () {
                            getList();
                        }, 1000);
                    }
                }
            });
        } else {
            //停止
            $.ajax({
                url: stopAll ? "/stopAll" : "/stop",
                type: "GET",
                data: {
                    url: url
                },
                dataType: "JSON",
                success: function (response) {
                    if (response.code === 200) {
                        alert(response.msg);
                        setTimeout(function () {
                            getList();
                        }, 1000);
                    }
                }
            });
        }
    }
</script>
<script src="/demo/flv.js"></script>
<script>
    var player;
    $(function () {
        getList();
        player = loadVideo();
    });

    // var player = document.getElementById('videoElement');


    function loadVideo(url) {
        if (flvjs.isSupported()) {
            player = flvjs.createPlayer({
                type: 'flv',
                isLive: true,
                url: url || 'http://localhost:8866/live?url=rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov'
            });
            player.attachMediaElement(videoElement);
            player.load(); //加载
            return player;
        }
    }

    function flv_start() {
        player.play();
    }

    function flv_change() {
        console.log(player);
        // changeStatus(0, true);
        // flv_destroy();
        var httpUrl = "http://localhost:8866/live?url=" + $("#videoSelect option:selected").data("url");
        player._mediaDataSource.url = httpUrl;
        player._statisticsInfo.url = httpUrl;
        player.pause();
        player.unload();
        player.load();
        player.play();
        // console.log(httpUrl);
        // player = loadVideo(httpUrl);
    }

    function flv_pause() {
        player.pause();
    }

    function flv_destroy() {
        if (player) {
            player.pause();
            player.unload();
            player.detachMediaElement();
            player.destroy();
            player = null;
        }
    }

    function flv_seekto() {
        player.currentTime = parseFloat(document.getElementsByName('seekpoint')[0].value);
    }
</script>
</html>